package com.xiaouyudeguang.common.log.listener;

import com.xiaouyudeguang.common.annotations.ConsumerForEvent;
import com.xiaouyudeguang.common.event.LogEvent;
import com.xiaouyudeguang.common.log.channel.LogEventChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.support.MessageBuilder;

import javax.validation.constraints.NotNull;

@Configuration
@EnableBinding({LogEventChannel.class})
@ConditionalOnClass(ConsumerForEvent.class)
public class LogEventApplicationListener implements ApplicationListener<LogEvent> {

    @Autowired(required = false)
    private LogEventChannel channel;

    @Override
    public void onApplicationEvent(@NotNull LogEvent logEvent) {
        channel.output().send(MessageBuilder.withPayload(logEvent).build());
    }
}
